package com.mccken.springframework.core.io;

import java.net.MalformedURLException;
import java.net.URL;

import cn.hutool.core.lang.Assert;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-11-30 15:55
 **/
public class DefaultResourceLoader implements ResourceLoader {

	@Override
	public Resource getResource(String location) {
		Assert.notNull(location, "Location must be not null");
		if (location.startsWith(CLASSPATH_URL_PREFIX)) {
			return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()));
		}
		else {
			try {
				URL url  = new URL(location);
				return new UrlResource(url);
			}
			catch (MalformedURLException e){
				return new FileSystemResource(location); 	
			}
		}
	}
}
